/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/commons/json/serializer/ShortSerializer.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: ShortSerializer.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.json.serializer;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @author elancom
 */
public class ShortSerializer implements ObjectSerializer {

	public static ShortSerializer instance = new ShortSerializer();

	@Override
	public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
		final SerializeWriter out = serializer.getWriter();

		final Number numberValue = (Number) object;

		if (numberValue == null) {
			if (out.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
				out.write('0');
			} else {
				out.writeNull();
			}
			return;
		}

		final short value = ((Number) object).shortValue();
		out.writeInt(value);

		if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
			out.write('S');
		}
	}
}
